/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.games.epkb.controller;

import it.polimi.games.epkb.controller.exceptions.NonexistentEntityException;
import it.polimi.games.epkb.entityClasses.StoredValues;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import it.polimi.games.epkb.entityClasses.GpiKpi;
import it.polimi.games.epkb.entityClasses.Component;
import javax.transaction.UserTransaction;

/**
 *
 * @author plebani
 */
public class StoredValuesJpaController implements Serializable {

    public StoredValuesJpaController(EntityManagerFactory emf) {
        this.utx = null;
        this.emf = emf;
    }

    public StoredValuesJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(StoredValues storedValues) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            GpiKpi idGPIKPI = storedValues.getIdGPIKPI();
            if (idGPIKPI != null) {
                idGPIKPI = em.getReference(idGPIKPI.getClass(), idGPIKPI.getId());
                storedValues.setIdGPIKPI(idGPIKPI);
            }
            Component idComponent = storedValues.getIdComponent();
            if (idComponent != null) {
                idComponent = em.getReference(idComponent.getClass(), idComponent.getId());
                storedValues.setIdComponent(idComponent);
            }
            em.persist(storedValues);
            if (idGPIKPI != null) {
                idGPIKPI.getStoredValuesCollection().add(storedValues);
                idGPIKPI = em.merge(idGPIKPI);
            }
            if (idComponent != null) {
                idComponent.getStoredValuesCollection().add(storedValues);
                idComponent = em.merge(idComponent);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(StoredValues storedValues) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            StoredValues persistentStoredValues = em.find(StoredValues.class, storedValues.getId());
            GpiKpi idGPIKPIOld = persistentStoredValues.getIdGPIKPI();
            GpiKpi idGPIKPINew = storedValues.getIdGPIKPI();
            Component idComponentOld = persistentStoredValues.getIdComponent();
            Component idComponentNew = storedValues.getIdComponent();
            if (idGPIKPINew != null) {
                idGPIKPINew = em.getReference(idGPIKPINew.getClass(), idGPIKPINew.getId());
                storedValues.setIdGPIKPI(idGPIKPINew);
            }
            if (idComponentNew != null) {
                idComponentNew = em.getReference(idComponentNew.getClass(), idComponentNew.getId());
                storedValues.setIdComponent(idComponentNew);
            }
            storedValues = em.merge(storedValues);
            if (idGPIKPIOld != null && !idGPIKPIOld.equals(idGPIKPINew)) {
                idGPIKPIOld.getStoredValuesCollection().remove(storedValues);
                idGPIKPIOld = em.merge(idGPIKPIOld);
            }
            if (idGPIKPINew != null && !idGPIKPINew.equals(idGPIKPIOld)) {
                idGPIKPINew.getStoredValuesCollection().add(storedValues);
                idGPIKPINew = em.merge(idGPIKPINew);
            }
            if (idComponentOld != null && !idComponentOld.equals(idComponentNew)) {
                idComponentOld.getStoredValuesCollection().remove(storedValues);
                idComponentOld = em.merge(idComponentOld);
            }
            if (idComponentNew != null && !idComponentNew.equals(idComponentOld)) {
                idComponentNew.getStoredValuesCollection().add(storedValues);
                idComponentNew = em.merge(idComponentNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = storedValues.getId();
                if (findStoredValues(id) == null) {
                    throw new NonexistentEntityException("The storedValues with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            StoredValues storedValues;
            try {
                storedValues = em.getReference(StoredValues.class, id);
                storedValues.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The storedValues with id " + id + " no longer exists.", enfe);
            }
            GpiKpi idGPIKPI = storedValues.getIdGPIKPI();
            if (idGPIKPI != null) {
                idGPIKPI.getStoredValuesCollection().remove(storedValues);
                idGPIKPI = em.merge(idGPIKPI);
            }
            Component idComponent = storedValues.getIdComponent();
            if (idComponent != null) {
                idComponent.getStoredValuesCollection().remove(storedValues);
                idComponent = em.merge(idComponent);
            }
            em.remove(storedValues);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<StoredValues> findStoredValuesEntities() {
        return findStoredValuesEntities(true, -1, -1);
    }

    public List<StoredValues> findStoredValuesEntities(int maxResults, int firstResult) {
        return findStoredValuesEntities(false, maxResults, firstResult);
    }

    private List<StoredValues> findStoredValuesEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(StoredValues.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public StoredValues findStoredValues(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(StoredValues.class, id);
        } finally {
            em.close();
        }
    }

    public int getStoredValuesCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<StoredValues> rt = cq.from(StoredValues.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
